import axios from 'axios'
import { Toast } from 'vant'

const ajax = axios.create({
  baseURL: '/api'
})

// 请求拦截
ajax.interceptors.request.use(config => {
  // 发送请求之前的拦截 显示一个加载框
  Toast({
    type: 'loading',
    message: '加载中。。。',
    duration: 0,
    loadingType: 'spinner'
  })
  return config
})
// 响应拦截
ajax.interceptors.response.use(resp => {
  // 请求成功后 清除加载框
  Toast.clear()
  if (resp.status === 200) {
    if (resp.data.code === 200) {
      // console.log(resp.data.data)
      return resp.data.data
    }
  }
  // 如果都不满足就返回这个
  return {
    code: 0,
    msg: 'fail'
  }
})
// 轮播图和列表页详情
export const getLists = (id, start) => ajax.get(`/tab/${id}?start=${start}`)
// 商城列表
export const getTabs = () => ajax.get('/tabs')
// 详情页
export const getDetail = id => ajax.get(`/detail?id=${id}`)
// 列表页
export const getListPage = (name, id, start) => ajax.get(`/${name}/${id}/items?start=${start}`)
// 搜索页
export const getSearch = () => ajax.get('/search/home')
// 搜索到的商品页
export const getSearchShop = (word) => ajax.get(`/search?word=${word}`)
